package com.cskaoyan.jdbc;

import com.mysql.jdbc.Driver;

import java.sql.*;

/**
 * 创建日期: 2022/06/01 11:24
 *
 * @author ciggar
 */
public class QueryDemo {


    static String url = "jdbc:mysql://localhost:3306/41th?useSSL=false&characterEncoding=utf8";
    static String username = "root";
    static String password = "123456";

    public static void main(String[] args) throws SQLException {

        // 1. 注册驱动  // 传入的是驱动包里面的Driver接口的实现
        DriverManager.registerDriver(new Driver());

        // 2. 建立连接 JDBC4Connection
        Connection connection = DriverManager.getConnection(url,username,password);

        // 3. 获取Statement对象  StatementImpl
        Statement statement = connection.createStatement();

        // 4. 发送SQL语句
        // 执行查询语句
        ResultSet resultSet = statement.executeQuery("select * from city");

        // 执行增删改语句
//        int affectedRows = statement.executeUpdate("insert into city values (1003,'苏州',32),(1004,'无锡',32)");


        // 5. 解析结果集
        // 向下移动游标
        resultSet.afterLast();

        while (resultSet.previous()) {

            int id = resultSet.getInt("id");

            String name = resultSet.getString("name");

            int pid = resultSet.getInt("pid");

            System.out.println("id:" + id + ", name:" + name + ", pid:" + pid);
        }

//        //6. 断开连接
////        resultSet.close();
////        statement.close();
////        connection.close();



    }
}
